//
//  AwardViewController.m
//  homeland
//
//  Created by rain on 14-5-12.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "AwardViewController.h"
#import "RecommendTableViewCell.h"
#import "ParticipateService.h"
#import "RecommendListItem.h"
#import "ParticipationSubViewController.h"

@interface AwardViewController () <ParticipateServiceDelegate,UITextFieldDelegate>
{
    BOOL ishasNext;
    int pageNo;
}
@property (nonatomic, strong) NSMutableArray *datas;
@property (nonatomic, weak) IBOutlet UIView *backView;
@property (nonatomic, weak) IBOutlet UITextField *mobileTextField;

@end

@implementation AwardViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"参与有奖";
    pageNo = 1;
    // Do any additional setup after loading the view from its nib.
    UIColor *color = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2f];
    self.mobileTextField.delegate = self;
    self.mobileTextField.returnKeyType = UIReturnKeySearch;

    self.datas = [[NSMutableArray alloc] init];
    self.backView.layer.masksToBounds = YES;
    self.backView.layer.cornerRadius = 8.0f;
    self.backView.layer.borderWidth = 0.5f;
    self.backView.layer.borderColor = [color CGColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView setBackgroundView:nil];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"_navBar"] forBarMetrics:UIBarMetricsDefault];

    int height = iPhone5 ? 0 : 88;
    [self.backView setFrame:CGRectMake(10, 64, 300, 425 - height)];
    [self.tableView setFrame:CGRectMake(0, 43, 300, 382 - height)];
    
    pageNo = 1;
    if (nil != self.datas) {
        [self.datas removeAllObjects];
    }
    [ParticipateService share].delegate = self;
    [[ParticipateService share] getRecommendList:nil pageNO:[NSString stringWithFormat:@"%d",pageNo]];
    
    self.navigationItem.rightBarButtonItem = customNavButton(self, @selector(rightButtonAction:));
    self.navigationItem.leftBarButtonItem = autoavBackButton(self, @selector(backTop));
}

- (void)backTop
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)backMethod
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Right Button
-(void)rightButtonAction:(id)sender
{
    ParticipationSubViewController *vc = [[ParticipationSubViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)rearchMobile:(id)sender
{
    [self.mobileTextField resignFirstResponder];
    [self.datas removeAllObjects];
    [[ParticipateService share] getRecommendList:self.mobileTextField.text pageNO:@"1"];
}

- (void)recommendList:(NSArray *)array hasNext:(BOOL)hasNext
{
    ishasNext = hasNext;
    for (NSDictionary *dict  in array) {
        RecommendListItem *item = [[RecommendListItem alloc] init];
        item.id = [[dict objectForKey:@"id"] intValue];
        item.insertTime = [dict objectForKey:@"insertTime"];
        item.recommendId = [[dict objectForKey:@"recommendId"] intValue];
        item.recommendName = [dict objectForKey:@"recommendName"];
        item.mobile = [dict objectForKey:@"mobile"];
        item.isRegister = [[dict objectForKey:@"isRegister"] boolValue];
        [self.datas addObject:item];
    }
    [self.tableView reloadData];
}

#pragma mark tableView
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.0f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (ishasNext) {
        return (self.datas.count + 1);
    }
    return self.datas.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *myIdentifier = @"recordDateCellIdentifier";
	RecommendTableViewCell *cell = (RecommendTableViewCell *)[tableView dequeueReusableCellWithIdentifier:myIdentifier];
	if (cell == nil)
	{
        [tableView registerNib:[UINib nibWithNibName:@"RecommendTableViewCell" bundle:nil] forCellReuseIdentifier:myIdentifier];
        cell = (RecommendTableViewCell *)[tableView dequeueReusableCellWithIdentifier:myIdentifier];
    }
    [cell setInitSubView];
    if (indexPath.row == self.datas.count  && ishasNext) {
        cell.moreLabel.hidden = NO;
    } else {
        UIColor *color = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2f];
        
        cell.layer.masksToBounds = YES;
        cell.layer.borderWidth = 0.1f;
        cell.layer.borderColor = [color CGColor];
        
        [cell setBackgroundColor:[UIColor clearColor]];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell updateDetial:[self.datas objectAtIndex:indexPath.row]];
        [cell setFrame:CGRectMake(0, 0, 300, 60)];

    }
    

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.datas.count && ishasNext) {
        pageNo += 1;
        [[ParticipateService share] getRecommendList:nil pageNO:[NSString stringWithFormat:@"%d",pageNo]];

    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self rearchMobile:nil];
    [textField resignFirstResponder];
    return YES;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
